package com.hsq.ndmall.user.service.impl;

import org.springframework.stereotype.Service;

import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hsq.ndmall.common.utils.PageUtils;
import com.hsq.ndmall.common.utils.Query;

import com.hsq.ndmall.user.dao.AddressDao;
import com.hsq.ndmall.user.entity.AddressEntity;
import com.hsq.ndmall.user.service.AddressService;


@Service("addressService")
public class AddressServiceImpl extends ServiceImpl<AddressDao, AddressEntity> implements AddressService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AddressEntity> page = this.page(
                new Query<AddressEntity>().getPage(params),
                getQueryWrapper(params)
        );

        return new PageUtils(page);
    }

    private QueryWrapper getQueryWrapper(Map<String, Object> params) {
        System.out.println(params+"===============");
        QueryWrapper<AddressEntity> queryWrapper = new QueryWrapper<>();
        if (params.containsKey("user_id")) {
            queryWrapper.lambda().eq(AddressEntity::getUserId,params.get("user_id"));
        }
        if(params.containsKey("is_default")){
            queryWrapper.lambda().eq(AddressEntity::getIsDefault,params.get("is_default"));
        }
        if(params.containsKey("id")){
            queryWrapper.lambda().eq(AddressEntity::getId,params.get("id"));
        }
        return queryWrapper;
    }
}
